package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.entity.Employee;
import com.itheima.reggie.service.CategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @Author 669
 * @CreateDate 2023/2/27 11:19
 * @Version 1.0
 */
@Slf4j
@RestController
@RequestMapping("/category")
public class CategoryController {


    @Autowired
    private CategoryService categoryService;


    /**
     * 获取全部菜品信息并分页
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/page")
    public R<Page> getCategoryPage(int page, int pageSize){
        //构造分页构造器
        Page pageInfo = new Page(page,pageSize);
        LambdaQueryWrapper<Category> wrapper = new LambdaQueryWrapper<>();
        wrapper.orderByAsc(Category::getSort);
        categoryService.page(pageInfo,wrapper);
        log.info("获取菜品信息成功");
        return R.success(pageInfo);

    }

    /**
     * 通过id获取菜品信息
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R<Category> queryCategoryById(@PathVariable Long id){
        Category category = categoryService.getById(id);
        log.info("执行获取菜品信息");
        if (category!=null){
            return R.success(category);
        }
        return R.error("获取菜品信息失败");
    }

    /**
     * 修改菜品信息
     * @param category
     * @return
     */

    @PutMapping
    public R<String> editCategory(@RequestBody Category category){
        categoryService.updateById(category);
        log.info("执行修改菜品信息成功");
        return R.success("修改菜品信息成功");
    }

    /**
     * 添加菜品信息
     * @param category
     * @return
     */
    @PostMapping
    public R<String> addCategory(@RequestBody Category category){
        boolean b = categoryService.save(category);
        log.info("执行添加菜品信息");
        if (b){
            return R.success("添加菜品成功");
        }return R.error("添加菜品失败");
    }

    /**
     * 删除菜品
     * @param
     * @return
     */
    @DeleteMapping
    public R<String> deleCategory(Long ids){
        log.info("删除分类，id为：{}",ids);
        categoryService.remove(ids);
        return R.success("分类信息删除成功");

    }

    /**
     * 根据条件查询分类数据
     * @param category
     * @return
     */
    @GetMapping("/list")
    public R<List<Category>> getCategoryList(Category category){
        LambdaQueryWrapper<Category> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(category.getType()!=null,Category::getType,category.getType());
        wrapper.orderByAsc(Category::getSort).orderByDesc(Category::getUpdateTime);
        List<Category> list = categoryService.list(wrapper);
        return R.success(list);
    }
}
